package com.gitee.taotaojs.mybootsuper.controller;

import com.gitee.taotaojs.exception.MyException;
import com.gitee.taotaojs.mybootsuper.bean.AppBack;
import com.gitee.taotaojs.mybootsuper.bean.BaseBean;
import com.gitee.taotaojs.mybootsuper.service.MyComService;
import com.gitee.taotaojs.util.reflect.EntityUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 通用Controller
 * @author TaoTaojs
 * @date 2020年03月20日15:49:46
 * <p>Description</p>
 * 基本Contorller方法，可直接使用URL调用（慎重继承）
 * 关联类:
 * 参考链接：
 */
public class MyComController<T extends BaseBean> implements IMyBasicController {

    @Autowired
    protected MyComService<T> beanService;


    @ApiOperation(
            value="获取数据库中所有的对象", httpMethod = GET,
            notes = ""
    )
    @GetMapping("/list")
    @ResponseBody
    public AppBack<List<T>> list(){
        List<T> list = beanService.findAll();
        return new AppBack<>(list);
    }


    @ApiOperation(
            value="查询单个对象具体数据", httpMethod = GET,
            notes = "如果找到了多条将会报错"

    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entity", value = "实体类对象", required = true, paramType="query")
    })
    @GetMapping("/select")
    @ResponseBody
    public AppBack<T> select(T entity){
        T entityDB = beanService.findObject(entity);
        if(entityDB == null){
            throw new MyException("没有数据");
        }
        return new AppBack<>(entityDB);
    }


    @ApiOperation(
            value="新增单个对象", httpMethod = POST,
            notes = ""
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entity", value = "实体类对象", required = true, paramType="query")
    })
    @PostMapping("/add")
    @ResponseBody
    public AppBack<T> add(T entity){
        if(EntityUtil.isEmpty(entity)){
            throw new MyException("插入对象为空，请检查");
        }
        beanService.addOrUpdateObject(entity);
        return new AppBack<>(entity);
    }


    @ApiOperation(
            value="更新单个对象具体数据", httpMethod = PUT,
            notes = ""
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entity", value = "实体类对象", required = true, paramType="query")
    })
    @PutMapping("/update")
    @ResponseBody
    public AppBack<T> update(T entity){
        if(entity.getId() == null){
            throw new MyException("更新对象id不能为空");
        }
        beanService.addOrUpdateObject(entity);
        return new AppBack<>(entity);
    }


    @ApiOperation(
            value="删除单个对象", httpMethod = DELETE,
            notes = ""
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entity", value = "实体类对象", required = true, paramType="query")
    })
    @DeleteMapping("/remove")
    @ResponseBody
    public AppBack<T> remove(T entity){
        if(entity.getId() == null){
            throw new MyException("删除对象id不能为空");
        }
        beanService.deleteObject(entity);
        return new AppBack<>(entity);
    }


    @ApiOperation(
            value="查询多个对象的具体数据", httpMethod = GET,
            notes = ""
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entity", value = "实体类对象", required = true, paramType="query")
    })
    @GetMapping("/findByObjList")
    @ResponseBody
    public AppBack<List<T>> findByObjList(T entity){
        List<T> list = beanService.findObjectList(entity);
        return new AppBack<>(list);
    }



}
